package com.hllxd.hism.star.basic.service.controller2;

import com.hllxd.hism.star.basic.service.exception.BusinessException;
import com.hllxd.hism.star.basic.service.service.AlertRuleService;
import com.hllxd.hism.framework.httprpc.service.*;
import com.hllxd.hism.star.basic.service.model.AlertRuleEntity;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 计算任务白名单管理
 *
 * @author xiyong.lxy
 * @date 2020-10-12 16:51
 */
@Slf4j
@RestGrpcService
@RestGrpcMapping("/v1/alert_rule")
public class AlertRuleController2 {

    @Autowired
    private AlertRuleService alertRuleService;

    @Data
    public static class Param {
        public Long id;
        public String ids;
        public String ruleCode;
        private String phoneNumber;
    }

    @RestGrpcMapping("/listAlertPolicy.do")
    public NTSTATUS listAlertPolicy(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            List<AlertRuleEntity> items = alertRuleService.listAlertPolicy();
            log.info("items: {}.", items.size());
            result.setData(items);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e.toString(), params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e.toString(), params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/getAlertPolicy.do")
    public NTSTATUS getAlertPolicy(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Param aa = params.driverObject(Param.class);
            AlertRuleEntity item = alertRuleService.getAlertPolicy(aa.id, aa.ruleCode);
            result.setData(item);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e.toString(), params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e.toString(), params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/setAlertPolicy.do")
    public NTSTATUS setAlertPolicy(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            AlertRuleEntity aa = params.driverObject(AlertRuleEntity.class);
            Object res = alertRuleService.setAlertPolicy(aa);
            result.setData(res);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e.toString(), params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e.toString(), params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/deleteAlertPolicy.do")
    public NTSTATUS deleteAlertPolicy(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Param aa = params.driverObject(Param.class);
            Boolean ret = alertRuleService.deleteAlertPolicy(aa.ids);
            result.setData(ret);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e.toString(), params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e.toString(), params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }
}
